package com.fwh.web.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.fwh.web.utils.RequestUtil;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * qq登录验证
 * @return
 * @author:fwh [2019/4/3 && 16:31]
 */
@Controller
@RequestMapping("/qq")
@Slf4j
public class QqLoginController {
    @GetMapping("/login")
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        httpServletResponse.setContentType("text/html;charset=utf-8");
        try {
            String requestUrl = new Oauth().getAuthorizeURL(httpServletRequest);
            log.info("请求QQ登录地址为:{}",requestUrl);
            httpServletResponse.sendRedirect(requestUrl);
            log.info("跳转到QQ账号登录授权...");
        } catch (Exception e) {
            log.error("QQ授权登录连接异常为:{}",e);
        }
        return null;
    }


    /**
     * @param request
     * @param response
     * @return void
     * @author:fwh [2019/3/28_10:18]
     * @Des: 处理QQ登录成功后业务逻辑
     */
    @GetMapping("/afterLogin")
    public String handleResponse(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html; charset=utf-8");
        String requestIP = RequestUtil.getRequestIP(request);
        try {
            AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
            String accessToken = null, openID = null;
            long tokenExpireIn = 0L;
            if ("".equals(accessTokenObj.getAccessToken())) {
                log.error("用户已取消授权||登录服务异常");
            } else {
                //用户的token
                accessToken = accessTokenObj.getAccessToken();
                //用户的存活时间
                tokenExpireIn = accessTokenObj.getExpireIn();
                log.info("用户授权存活时间最大为:{}", tokenExpireIn);
                //用户的openId--唯一值与qq对应.
                openID = new OpenID(accessToken).getUserOpenID();
                //获取用户信息
                UserInfoBean userInfoBean = new UserInfo(accessToken, openID).getUserInfo();
                //0为正常情况.
                if (userInfoBean.getRet() == 0) {
                    log.info("QQ用户token为:{},openID为:{}", accessToken, openID);
                    log.info("QQ用户信息为:{}", userInfoBean);
                    String nickName = userInfoBean.getNickname();
                    //查看是否关联已有系统账户查询登录.
                    log.info("昵称:{}",nickName);
                    log.info("请求IP为:{}",requestIP);
                    return "redirect:/index";
//                    Map<String, Object> connectedAccountMap = isConnectedAccount(openID, accessToken, requestIP);
//                    if ((Boolean) connectedAccountMap.get(UserConstants.RESULT)) {
//                        //设置昵称前端展示
//                        LoginUser loginUser = ShiroUtil.getLoginUser();
//                        loginUser.setNickName(nickName);
//                        ShiroUtil.setUser(loginUser);
//                        return "redirect:/static/html/common/frame.html";
//                    }
                } else {
                    log.error("未能正取获取QQ登录用户信息,原因是:{}", userInfoBean.getMsg());
                }
            }
        } catch (Exception e) {
            log.error("QQ登录系统异常为:{}", e);
        }
        return "redirect:/login";
    }
}
